﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SupportLibrary;
using Common.Logging;
using Automation.Protocols.Dao;
using Automation.Protocols.Model;
using SupportLibrary.Message;
namespace Automation.Dal.Excel.Dao
{
    public class TestMapDao:TestMapBase
    {
        private ILog log = LogManager.GetLogger(typeof(TestPlanDao));
         private ExcelProvider _exProvider;
        private string _Path;
        public TestMapDao(string path)
        {
            _Path = path;
            _exProvider = ExcelProvider.Create(@path);
            if (_exProvider == null)
                //MessageShow.FileDoesNotExist(path);
                throw new Exception(path + "doesnot exist!");
        }

        ~TestMapDao()
        {
            _exProvider.SubmitChanges();
        }

        public override List<TestMap> getListTMap()
        {
            List<TestMap> list = new List<TestMap>();
            // Console.WriteLine("READING SHEET TData.");
            log.Debug(Resources.Information.msg_ReadingSheetTMap);
            try
            {
                if (_exProvider != null)
                {
                    var TestMaps = _exProvider.GetSheet<Automation.Dal.Excel.Model.TestMap>();
                    int i = 0;
                    if (TestMaps != null)
                    {
                        foreach (Automation.Dal.Excel.Model.TestMap item in TestMaps)
                        {
                            if (item.ID != null)
                            {
                                i++;
                                if (item.isValid())
                                    list.Add(new TestMap()
                                    {
                                        ID = item.ID,
                                        Page = item.Page,
                                        ControlName = item.ControlName,
                                        TagName= item.TagName,
                                        AttributeValue = item.AttributeValue,
                                        Xpath = item.Xpath
                                    });
                                else
                                {
                                    //MessageShow.InvalidRecord(i + 1);
                                    throw new Exception("Record " + (i + 1) + " is invalid!");
                                }
                            }
                        }
                    }
                    //Console.WriteLine(i + " records were read in sheet TData.");
                    log.Debug(Message.ReplaceMessage(Resources.Information.msg_NumofRecord, ByKey.ByNumber(), i.ToString()));
                }
            }
            catch (Exception e)
            {
                log.Error(Resources.ErrorMessage.ReadTDataSheetErr + e.GetType().ToString() + e.Message);
                //MessageShow.ExceptionOccur("Read TData sheet exception.", e);
                throw new Exception(Resources.ErrorMessage.ReadSheetTMapSetErr1 + e.GetType().ToString());
            }
            return list;
        }

        public override void InsertTMap(List<TestMap> listMap)
        {
            try
            {
                if (_exProvider != null)
                {
                    foreach (var i in listMap)
                    {
                        Automation.Dal.Excel.Model.TestMap item = new Automation.Dal.Excel.Model.TestMap()
                        {
                            ID = i.ID,
                            Page = i.Page,
                            ControlName = i.ControlName,
                            TagName = i.TagName,
                            AttributeValue = i.AttributeValue,
                            Xpath = i.Xpath
                        };
                        _exProvider.GetSheet<Automation.Dal.Excel.Model.TestMap>().InsertOnSubmit(item);
                    }
                    //_exProvider.SubmitChanges();

                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Generate TData sheet exception.", e);
                log.Error(Resources.ErrorMessage.InsertSheetTMapErr + e.GetType().ToString() + e.Message);
                throw new Exception(Resources.ErrorMessage.InsertSheetTMapErr + e.GetType().ToString());
            }
        }

        public override void UpdateTMap(List<TestMap> listMap)
        {
            try
            {
                if (_exProvider != null)
                {
                    var currentMaps = _exProvider.GetSheet<Automation.Dal.Excel.Model.TestMap>();
                    foreach (var i in listMap)
                    {
                        try
                        {


                            foreach (Automation.Dal.Excel.Model.TestMap item in currentMaps)
                            {
                                if (item.ID == i.ID)
                                {
                                    if (item.isValid())
                                    {

                                        item.ID = i.ID;
                                        item.TagName = i.TagName;
                                        item.Page = i.Page;
                                        item.ControlName = i.ControlName;
                                        item.AttributeValue = i.AttributeValue;
                                        item.Xpath = i.Xpath;

                                        _exProvider.GetSheet<Automation.Dal.Excel.Model.TestMap>().UpdateOnSubmit(item);
                                        break;

                                    }
                                }
                            }

                        }
                        catch (Exception ex)
                        {
                            log.Debug(Resources.ErrorMessage.UpdateTDataErr + ex.GetType().ToString() + ex.Message);
                            throw new Exception(Resources.ErrorMessage.UpdateTDataErr + ex.GetType().ToString());
                        }
                    }
                    _exProvider.SubmitChanges();

                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Update TStep sheet exception.", e);
                log.Error(Resources.ErrorMessage.UpdateTMapErr + e.GetType().ToString() + e.Message);
                throw new Exception(Resources.ErrorMessage.UpdateTMapErr + e.GetType().ToString());
            }
        }

        public override int getIndex()
        {
            int i = 0;
            try
            {
                if (_exProvider != null)
                {
                    var TestMaps = _exProvider.GetSheet<Automation.Dal.Excel.Model.TestMap>();
                    if (TestMaps != null)
                    {
                        foreach (Automation.Dal.Excel.Model.TestMap item in TestMaps)
                        {
                            if (item.ID != null)
                                i++;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Read TData sheet exception.", e);
                log.Error(Resources.ErrorMessage.ReadSheetTMapSetErr1 + e.GetType().ToString() + e.Message);
                throw new Exception(Resources.ErrorMessage.ReadSheetTMapSetErr1 + e.GetType().ToString());
            }
            return i + 1;
        }
    }
}
